﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConcordWinManager
{
    /// <summary>
    /// Оплата
    /// </summary>
    public class Oplata
    {
        /// <summary>
        /// Идентификатор оплаты
        /// </summary>
        public int id_oplata;

        /// <summary>
        /// Сумма оплтаы
        /// </summary>
        public decimal Summ;

        /// <summary>
        /// Дата оплаты
        /// </summary>
        public DateTime DateOplata;

        /// <summary>
        /// Идентификатор договора
        /// </summary>
        public int id_contract;

        /// <summary>
        /// ИД консультанта, провевшего оппату
        /// </summary>
        public int id_consultant;

        /// <summary>
        /// Договор
        /// </summary>
        public Contract contract;

        /// <summary>
        /// Консультант
        /// </summary>
        public Consultant consultant;

        /// <summary>
        /// Основание оплаты
        /// </summary>
        public string Definition;

        /// <summary>
        /// Номер квитанции в формате, принятом на предприятии
        /// DD-MM-YYYY-N/M
        /// M - порядковый номер оплаты по договору
        /// </summary>
        public string OplataNum;

        /// <summary>
        /// Шаблон квитанции об оплате
        /// </summary>
        public Template template_check;

        /// <summary>
        /// ИД шаблона квитанции об оплате
        /// </summary>
        public int id_template;

        /// <summary>
        /// Конструктор по умолчанию
        /// </summary>
        public Oplata()
        {
            this.contract = new Contract();
            this.template_check = new Template();
            this.consultant = new Consultant();
        }

        /// <summary>
        /// Добаление в БД
        /// </summary>
        public void Insert()
        {
            DataSet ds = new DataSet();
            string query1 = string.Format("SELECT COUNT(id_oplata) AS [CountOplat] FROM dbo.Oplata WHERE id_contract = {0}",
                this.id_contract);

            DB.query_table(query1, "dbo.Oplata", ref ds);

            int countOplat = int.Parse(ds.Tables[0].Rows[0]["CountOplat"].ToString());

            this.OplataNum = string.Format(@"{0}/{1}",
                this.contract.ContractNumber,
                countOplat + 1);

            string query2 = string.Format("INSERT INTO dbo.Oplata (Summ, id_contract, Date_oplata, Definition, id_consultant, OplataNumber, id_template) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                 this.Summ.ToString().Replace(',', '.'),
                 this.id_contract,
                 string.Format("{0}-{1}-{2}", this.DateOplata.Year, this.DateOplata.Month, this.DateOplata.Day),
                 this.Definition,
                 Settings.consultant.id_consultant,
                 this.OplataNum,
                 this.template_check.id_template);

            DB.execute_sql(query2);
        }

        public Oplata SelectById(int id)
        {
            DataSet ds = new DataSet();
            string tblName = "dbo.Oplata";
            DB.query_table("SELECT * FROM dbo.Oplata WHERE id_oplata = " + id, tblName, ref ds);

            if (int.TryParse(ds.Tables[tblName].Rows[0]["id_oplata"].ToString(), out this.id_oplata)) { ;}
            if (int.TryParse(ds.Tables[tblName].Rows[0]["id_contract"].ToString(), out this.id_contract)) { ;}
            this.OplataNum = ds.Tables[tblName].Rows[0]["OplataNumber"].ToString();
            this.Summ = decimal.Parse(ds.Tables[tblName].Rows[0]["Summ"].ToString());
            this.Definition = ds.Tables[tblName].Rows[0]["Definition"].ToString();
            if (DateTime.TryParse(ds.Tables[tblName].Rows[0]["Date_Oplata"].ToString(), out this.DateOplata)) { ;}
            if (int.TryParse(ds.Tables[tblName].Rows[0]["id_consultant"].ToString(), out this.id_consultant)) { ;}
            if (int.TryParse(ds.Tables[tblName].Rows[0]["id_template"].ToString(), out this.id_template)) { ;}

            this.contract.SelectById(this.id_contract);
            this.consultant.SelectById(this.id_consultant);
            this.template_check.SelectById(this.id_template);
            

            return this;
        }

        public void SelectAll(ref DataSet ds)
        {
            ;
        }

        public void Update()
        {
            ;
        }

        public void DeleteById()
        {
            ;
        }
    }
}
